<template>
  <!-- 仲裁费 -->
  <view class="arbitration">
    <view class="calculator">
      <view class="display">
        <view class="table" v-if="state.isShowTable && !state.isSiingle">
          <view class="tr">
            <text class="td"></text>
            <text class="td">{{ state.result.firstFeeName }}</text>
            <text class="td">{{ state.result.secondFeeName }}</text>
            <text class="td">{{ state.result.sumFeeName }}</text>
          </view>
          <view class="tr">
            <view class="td">最小</view>
            <view class="td">{{ state.result.minAcceptFee }}</view>
            <view class="td">{{ state.result.minProcessFee }}</view>
            <view class="td">{{ state.result.minSum }}</view>
          </view>
          <view class="tr">
            <view class="td">最大</view>
            <view class="td">{{ state.result.maxAcceptFee }}</view>
            <view class="td">{{ state.result.maxProcessFee }}</view>
            <view class="td">{{ state.result.maxSum }}</view>
          </view>
        </view>
        <view class="table-single" v-if="state.isShowTable && state.isSiingle">
          <view class="tr">
            <text class="td">{{ state.result.firstFeeName }}</text>
            <text class="td">{{ state.result.secondFeeName }}</text>
            <text class="td">{{ state.result.sumFeeName }}</text>
          </view>
          <view class="tr">
            <view class="td">{{ state.result.midAcceptFee }}</view>
            <view class="td">{{ state.result.midProcessFee }}</view>
            <view class="td">{{ state.result.midSum }}</view>
          </view>
        </view>
      </view>
      <view class="condition">
        <view class="cell">
          <text class="label">省份</text>
          <view class="value">
            <calculator-select v-model="state.province" :localdata="state.provinceRange" @change="provinceChange"
              :clear="false"></calculator-select>
          </view>
        </view>
        <view class="cell">
          <text class="label">仲裁机构</text>
          <view class="value">
            <calculator-select v-model="state.arbitration" :localdata="state.arbitrationRange" @change="arbitrationChange"
              :clear="false"></calculator-select>
          </view>
        </view>
        <view class="cell">
          <text class="label">争议金额</text>
          <view class="value">
            <calculator-select v-model="state.isControversial" :localdata="state.controversialRange"
              @change="controversialChange" :clear="false"></calculator-select>
          </view>
        </view>
        <view class="cell" v-if="state.isControversial == 0">
          <text class="label">涉及金额</text>
          <view class="amount">
            <view class="amount-input">
              <input type="digit" placeholder="请输入金额" v-model="state.amount" @input="inputAmount"
                :maxlength="state.moneyMaxLeng" :class="input" />
            </view>
            <view class="unit">
              <calculator-select v-model="state.unit" :localdata="state.unitRange" @change="unitChange"
                :clear="false"></calculator-select>
            </view>
          </view>
        </view>
        <view class="calculate" @tap="calculate">计算</view>
      </view>
    </view>
    <view class="point">
      <view class="icon">!</view>
      <view class="text">
        <text>根据</text>
        <text class="file">《广东广州委员会仲裁收费》</text>
        <text>文件准确计算，结果供您参考。</text>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
// @ts-nocheck
import { reactive, onMounted, nextTick, defineEmits } from 'vue';
import { getMenu, calculator } from '@/api/arbitration';
import calculatorSelect from '@/components/calculator-select/calculator-select.vue';

const emit = defineEmits(['changeExplanation']);

const state = reactive({
  province: '',
  //菜单
  provinceRange: [], // 省数据
  arbitration: '',
  arbitrationRange: [], // 仲裁机构/*
  isControversial: 0,
  controversialRange: [
    { id: 0, title: '有争议金额' },
    { id: 1, title: '无争议金额' },
  ],
  unit: 0,
  unitRange: [
    { id: 0, title: '元' },
    { id: 1, title: '万元' },
  ],
  amount: '',
  moneyMaxLeng: 8,
  result: {},
  //判断计算结果是否显示
  isSiingle: false,
  isShowTable: false,
});

onMounted(async () => {
  const data = await getMenu();
  state.provinceRange = data;
});

function initData() {
  state.result = {};
  state.isSiingle = false;
  state.isShowTable = false;
}

function inputAmount(e: any) {
  initData();
  nextTick(() => {
    let val = e.target.value.toString();
    val = val.replace(/[^\d.]/g, ''); // 清除"数字"和"."以外的字符
    val = val.replace(/\.{2,}/g, '.'); // 只保留第一个. 清除多余的
    val = val.replace(/^0+\./g, '0.');
    val = val.match(/^0+[1-9]+/) ? (val = val.replace(/^0+/g, '')) : val;
    val = val.match(/^\d*(\.?\d{0,2})/g)[0] || '';

    if (val.includes('.')) {
      let numDian = val.toString().split('.')[1].length;
      if (numDian === 2) {
        state.moneyMaxLeng = val.length;
      }
    } else {
      state.moneyMaxLeng = 8;
    }
    state.amount = val;
  });
}

// 改变省份
function provinceChange(e: any) {
  initData();
  state.provinceRange.forEach((item: any) => {
    if (item.id == e) {
      state.arbitrationRange = item.menuList;
    }
  });
}

// 改变仲裁机构
function arbitrationChange(e: any) {
  initData();
}

// 有无争议金额
function controversialChange(e: any) {
  initData();
}

function unitChange(e: any) {
  initData();
}

async function calculate() {
  // unit 0-元  1-万
  const { province, arbitration, isControversial, unit, amount } = state;

  if (!province) {
    return uni.showToast({
      title: '请选择省份',
      icon: 'none',
    });
  }

  if (!arbitration) {
    return uni.showToast({
      title: '请选择仲裁机构',
      icon: 'none',
    });
  }

  if (isControversial == 0 && !amount) {
    return uni.showToast({
      title: '请输入涉及金额',
      icon: 'none',
    });
  }

  const params = {
    committeeId: arbitration, // 仲裁机构id
    money: amount, // 金额
    unit: unit,
  };

  const data = await calculator(params);
  state.result = data;
  state.isShowTable = true;

  emit('changeExplanation', {
    isShowTable: true,
    explains: data.explains,
  });

  if (data.midSum) {
    state.isSiingle = true;
  }
}
</script>

<style lang="scss" scoped>
.calculator {
  margin: 0 24rpx;
  padding: 20rpx;
  width: 702rpx;
  min-height: 656rpx;
  background: #bdd1fd;
  box-shadow: inset 8rpx 8rpx 8rpx 0rpx rgba(106, 136, 190, 0.1);
  border-radius: 20rpx;
  border: 2rpx solid rgba(81, 134, 208, 0.3);

  .display {
    min-height: 196rpx;
    background: #dfe9ff;
    padding: 16rpx;
    box-shadow: inset 4rpx 8rpx 8rpx 0rpx rgba(106, 136, 190, 0.2), inset -8rpx -8rpx 8rpx 0rpx rgba(255, 255, 255, 0.5);
    border-radius: 20rpx;
    border: 2rpx solid;
    border-image: linear-gradient(180deg, rgba(81, 134, 208, 0.1), rgba(91, 139, 250, 0.2)) 2 2;

    .table {
      width: 100%;
      height: 100%;
      color: #777;
      border: 2rpx solid #fff;

      .tr {
        display: flex;
        width: 100%;
        height: 124rpx;

        &:not(:first-child) {
          height: 80rpx;
        }

        &:not(:last-child) {
          border-bottom: 2rpx solid #fff;
        }

        .td {
          width: 90rpx;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          text-align: center;

          &:not(:first-child) {
            width: 182rpx;
          }

          &:not(:last-child) {
            border-right: 2rpx solid #fff;
          }
        }
      }
    }

    .table-single {
      width: 100%;
      height: 100%;
      color: #777;
      border: 2rpx solid #fff;

      .tr {
        display: flex;
        width: 100%;
        height: 124rpx;

        &:not(:first-child) {
          height: 80rpx;
        }

        &:not(:last-child) {
          border-bottom: 2rpx solid #fff;
        }

        .td {
          width: 210rpx;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          text-align: center;
          line-height: 22px;

          &:not(:first-child) {
            width: 212rpx;
          }

          &:not(:last-child) {
            border-right: 2rpx solid #fff;
          }
        }
      }
    }
  }

  .condition {
    margin-top: 20rpx;
    min-height: 400rpx;
    padding: 20rpx;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 20rpx;

    .cell {
      height: 80rpx;
      display: flex;
      justify-content: space-between;

      .label {
        width: 124rpx;
        padding: 0 8rpx;
        background: rgba(255, 255, 255, 0.4);
        border-radius: 10rpx;
        border: 0rpx solid #f3f5f8;
        color: #5186d0;
        font-size: 26rpx;
        font-weight: 500;
        line-height: 80rpx;
        text-align-last: justify;
      }

      .value {
        width: 480rpx;
        height: 80rpx;
        background: #fff;
        border-radius: 10rpx;
        border: 0rpx solid #5186d0;

        :deep(.uni-select) {
          height: 80rpx !important;
          border: none;
          padding: 0 20rpx;
        }
      }

      .amount {
        width: 480rpx;
        display: flex;

        .amount-input {
          flex: 1;
          background-color: #fff;
          border-radius: 10rpx;
          margin-right: 20rpx;

          .input {
            display: flex;
            width: 100%;
            height: 80rpx;
            padding: 0 20rpx;
          }
        }

        .unit {
          background-color: #fff;
          width: 160rpx;
          border-radius: 10rpx;
        }

        :deep(.uni-select) {
          width: 160rpx;
          height: 80rpx !important;
          border: none;
          padding: 0 20rpx;
        }
      }

      &:not(:first-child) {
        margin-top: 20rpx;
      }
    }

    .calculate {
      margin-top: 20rpx;
      height: 72rpx;
      line-height: 72rpx;
      text-align: center;
      background: #9db9fc;
      color: #fff;
      box-shadow: 0rpx 8rpx 8rpx 0rpx rgba(91, 139, 250, 0.1);
      border-radius: 10rpx;
    }
  }
}

.point {
  margin: 20rpx 24rpx 0 24rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 22rpx;
  color: #aaa;

  .icon {
    width: 28rpx;
    height: 28rpx;
    background: #eb6559;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    margin-right: 12rpx;
  }

  .text {
    display: flex;

    .file {
      color: #fe9820;
    }
  }
}
</style>
